package com.psp.sp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.sp.controller.res.BaseResult;
import com.psp.sp.controller.res.ListResult;
import com.psp.sp.controller.res.ObjectResult;
import com.psp.sp.controller.res.bean.ROrderDetailBean;
import com.psp.sp.controller.res.bean.ROrderListBean;
import com.psp.sp.controller.springmvc.req.GetOrderDetailParam;
import com.psp.sp.controller.springmvc.req.GetOrderListParam;
import com.psp.sp.controller.springmvc.req.GetReceiveOrderParam;
import com.psp.sp.controller.springmvc.req.GetRefuseOrderParam;
import com.psp.sp.service.OrderService;
import com.psp.sp.service.exception.ServiceException;
import com.psp.sp.service.impl.PageResult;

@Component
public class OrderController {

	@Autowired
	OrderService orderServiceImpl;

	/**
	 * 订单详情
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<ROrderDetailBean> detail(GetOrderDetailParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<ROrderDetailBean> result = new ObjectResult<>();
		String uid = "";
		String oid = param.getOid();
		ROrderDetailBean resbean = orderServiceImpl.getDetail(uid, oid);
		result.setData(resbean);
		result.setFlag(true);
		result.setToken(param.getToken());
		return result;
	}

	/**
	 * 订单列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<ROrderListBean> list(GetOrderListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<ROrderListBean> result = new ListResult<>();

		String uid = "";
		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 10;
		int state = NumUtil.toInt(param.getState(), 0);

		PageResult<ROrderListBean> resList = orderServiceImpl.getList(page, pageSize, uid, state);

		result.setData(resList.getData());
		result.setTotalSize(resList.getCount());
		result.setToken(param.getToken());
		result.setPage(page);
		return result;
	}

	/**
	 * 接收订单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult receive(GetReceiveOrderParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String oid = param.getOid();
			String uid = "";
			boolean flag = orderServiceImpl.receive(uid, oid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(param.getToken());
		return result;
	}

	/**
	 * 拒绝订单
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult refuse(GetRefuseOrderParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			String oid = param.getOid();
			String uid = "";
			boolean flag = orderServiceImpl.refuse(uid, oid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(param.getToken());
		return result;
	}

}
